package cn.edu.scau.cmi.zengGuanSheng.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.scau.cmi.zengGuanSheng.hibernate.domain.Student;

public class StudentDao extends BaseHibernateDao {
	
	//添加学生
	public static void addStudent(Student stu) {
		Session s = getSession();
		s.save(stu);
		commitTransactionAndClose(s);
	}
	//删除学生
	public static Student deleteStudent(Student stu) {
		Session s = getSession();
//		Student stu1 = (Student)s.get(Student.class, stu.getId());
		Student stu1 = findStudentById(stu);
		s.delete(stu1);
		commitTransactionAndClose(s);
		return stu1;
	}
	
	//根据学号id查询学生
	public static Student findStudentById(Student stu) {
		Session s = getSession();
		Student stu1 = (Student)s.get(Student.class, stu.getId());
		commitTransactionAndClose(s);
		return stu1;
	}
	//保存修改的学生信息
	public static void saveEdit(Student stu) {
		Session s = getSession();
		s.update(stu);
		commitTransactionAndClose(s);
	}
	
	//根据学生姓名查找学生
	public static Student findStudentByName(Student t) {
		Session s = getSession();
		Query q =s.createQuery("from Student t where t.name = ?");
        q.setString(0, t.getName());
        List<Student> list= q.list();
        commitTransactionAndClose(s);
        return list.get(0);
	}
}
